package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.LeaveApprovalTeacherResponse;
import com.ycsj.goldmedalnewconcept.bean.LeaveApprovalTeacherResponse.LeaveApprovalListDetail;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//请假审批
public class LeaveApprovalActivity extends Activity implements OnClickListener {

	private ImageView iv_back;
	private ListView lv_leave;
	private List<LeaveApprovalListDetail> list = new ArrayList<LeaveApprovalListDetail>();
	private MyAdapter mAdapter;
	private TextView tv_empty;
	private String user_id;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_leaveapproval);
		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_back = (ImageView) findViewById(R.id.iv_back);
		lv_leave = (ListView) findViewById(R.id.lv_leave);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
	}

	private void initData() {
		String role = SPUtil.getString(LeaveApprovalActivity.this, "role", "");

		OkHttpClient mOkHttpClient = new OkHttpClient();
		// 创建一个Request
		final Request request = new Request.Builder()
				.url(Constant.SERVER_URL+"ManageMsg?type=16&c="+SPUtil.getString(this, "account", "")+"&role="+SPUtil.getString(this, "role", ""))
				.build();
		// new call13355556665
		Call call = mOkHttpClient.newCall(request);
		call.enqueue(callBack);

	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;

		}
	}

	class ViewHolder {
		private TextView tv_leavename;
		private TextView tv_time;
		private TextView tv_content;
		private TextView tv_yes;
		private TextView tv_call;
		private TextView tv_creattime;
		private ImageView iv_header;
	}

	class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater = null;

		private MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("ResourceAsColor")
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.list_leave, null);
				holder.iv_header = (ImageView) convertView
						.findViewById(R.id.iv_header);
				holder.tv_leavename = (TextView) convertView
						.findViewById(R.id.tv_leavename);
				holder.tv_time = (TextView) convertView
						.findViewById(R.id.tv_time);
				holder.tv_content = (TextView) convertView
						.findViewById(R.id.tv_content);
				holder.tv_yes = (TextView) convertView
						.findViewById(R.id.tv_yes);
				holder.tv_call = (TextView) convertView
						.findViewById(R.id.tv_call);
				holder.tv_creattime = (TextView) convertView
						.findViewById(R.id.tv_creattime);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();

			}
			holder.tv_leavename.setText(list.get(position).studentname);
			holder.tv_time.setText("请假时间：\n\r" + list.get(position).BEGIN_DATE
					+ "至" + list.get(position).END_DATE);
			holder.tv_content.setText("请假事由："+list.get(position).CONTENT);
			holder.tv_creattime.setText("创建时间："+list.get(position).CREATE_DATE);
			
			
			if(list.get(position).icon!=null&&!"".equals(list.get(position).icon)){
				Picasso.with(LeaveApprovalActivity.this)
				.load(list.get(position).icon).into(holder.iv_header);
			}
		
			
			if ("0".equals(list.get(position).STATUS)) {
				holder.tv_yes.setText("批准");
				holder.tv_yes.setBackgroundResource(R.drawable.shape_border_leave);
				holder.tv_yes.setTextColor(LeaveApprovalActivity.this.getResources().getColor(R.color.title));
				holder.tv_yes.setClickable(true);
				}else{
					holder.tv_yes.setBackgroundResource(R.drawable.shape_yipizhun_button);
					holder.tv_yes.setText("已批准");
					holder.tv_yes.setTextColor(R.color.base_black);
					holder.tv_yes.setClickable(false);
				}
			if("批准".equals(holder.tv_yes.getText().toString())){
				holder.tv_yes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						user_id = list.get(position).USER_ID;
						
						
						OkHttpClient mOkHttpClient = new OkHttpClient();
						// 创建一个Request
						final Request request = new Request.Builder()
								.url(Constant.SERVER_URL+"ManageMsg?type=14&rowid="+list.get(position).ROW_ID+"&c="+SPUtil.getString(LeaveApprovalActivity.this, "account", "")+"&role="+SPUtil.getString(LeaveApprovalActivity.this, "role", "")+"&status=1")
								.build();
						// new call13355556665
						Call call = mOkHttpClient.newCall(request);
						call.enqueue(callback2);
						holder.tv_yes.setText("已批准");
						holder.tv_yes.setBackgroundResource(R.drawable.shape_yipizhun_button);
						holder.tv_yes.setTextColor(R.color.base_black);
						list.get(position).STATUS = "1";
//						list.remove(position);
//						mAdapter.notifyDataSetChanged();
						
					}
				});
			}
		
			
			
			
			holder.tv_call.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					callPhone(list.get(position).USER_ID);
				}
			});
			return convertView;
		}
	}
	private void callPhone(final String tel) {
		AlertDialog.Builder builder = new AlertDialog.Builder(LeaveApprovalActivity.this);
		builder.setTitle("联系家长");
	  	builder.setMessage("拨打家长电话："+tel);
	  	builder.setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+tel));  
                startActivity(intent);  
			}
		});
	  	builder.setNegativeButton("取消", null);
	  	builder.show();
	}
	Handler handler = new Handler();
	Callback callBack = new Callback() {

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json = arg0.body().string();
			Log.e("test", json);
			Gson gson = new Gson();
			list.removeAll(list);

			if (json!=null) {
				LeaveApprovalTeacherResponse fromJson = gson.fromJson(json,
						LeaveApprovalTeacherResponse.class);
				if(fromJson!=null){
					if("-1".equals(fromJson.state)){
						handler.postDelayed(new Runnable() {

							@Override
							public void run() {
								tv_empty.setVisibility(tv_empty.VISIBLE);
							}
						}, 0);
					}else{
						list.addAll(fromJson.list);
					}
					
				}
				
				
						
//				for (int i = 0; i < fromJson.list.size(); i++) {
//					if ("0".equals(fromJson.list.get(i).STATUS)) {
//					}
//				}
				
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						mAdapter = new MyAdapter(LeaveApprovalActivity.this);
						lv_leave.setAdapter(mAdapter);
					}
				}, 0);
			}
		}

	};

	Callback callback2 = new Callback() {

		@Override
		public void onResponse(Response arg0) throws IOException {
			// TODO Auto-generated method stub
			String json = arg0.body().string();
			Log.e("test", json);
			if ("{\"state\":\"1\"}".equals(json)) {
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						
						Toast.makeText(LeaveApprovalActivity.this, "已批准", 0).show();
						pushMsg("1");
//						pushMsg("0");
					}

					
				}, 0);
			}
		}

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}
	};
	
	//推送
	private void pushMsg(String flag) {
		// TODO Auto-generated method stub
		OkHttpClient mOkHttpClient3 = new OkHttpClient();
		// 创建一个Request
		final Request request3 = new Request.Builder()
				.url(Constant.ycsj_count_MessageCenter+"?type=32&alias="+user_id+
		flag+"&content="+"您的请假申请已批准"+"&title="+"请假消息"+"&ptype=1")
				.build();
		
//http://www.moregolden.com/ycsj_count/MessageCenter?type=32&alias=182712617791&content=提交的请假申请已批准&title=请假成功了&ptype=1
		Call call3 = mOkHttpClient3.newCall(request3);
		call3.enqueue(callback3);
	}
	
	Callback callback3 = new Callback() {

		@Override
		public void onResponse(Response arg0) throws IOException {
			// TODO Auto-generated method stub
			String json = arg0.body().string();
			Log.e("jpush", json);
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
					}
				}, 0);
		}

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}
	};
}
























